package com.wsoft.system.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wsoft.core.exception.Asserts;
import com.wsoft.system.entity.SysResourceEntity;
import com.wsoft.system.mapper.SysResourceMapper;
import com.wsoft.system.query.resourse.ResourceInsertQuery;
import com.wsoft.system.query.resourse.ResourceListQuery;
import com.wsoft.system.query.resourse.ResourceUpdateQuery;
import com.wsoft.system.service.ISysResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.utils.ResourceUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台资源表 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年11月18日 17:13:44
 */
@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResourceEntity> implements ISysResourceService {

    @Override
    public void initAllResource() {
        //先清空
        ResourceUtil.resourceMap.clear();
        List<SysResourceEntity> list = list();
        //所有多语言配置放入内存中
        for (SysResourceEntity resource : list) {
            ResourceUtil.resourceMap.put(resource.getId() ,resource);
        }
    }

    @Override
    public boolean insert(ResourceInsertQuery query) {
        SysResourceEntity entity = Convert.convert(SysResourceEntity.class, query);
        QueryWrapper<SysResourceEntity> resourceWrapper =  new QueryWrapper<>();
        resourceWrapper.lambda().eq(SysResourceEntity::getUrl, entity.getUrl());
        if (count(resourceWrapper) > 0) {
            Asserts.fail("[sys.url.exist]");
        }
        return save(entity);
    }

    @Override
    public boolean update(ResourceUpdateQuery query) {
        SysResourceEntity entity = Convert.convert(SysResourceEntity.class, query);
        QueryWrapper<SysResourceEntity> resourceWrapper =  new QueryWrapper<>();
        resourceWrapper.lambda().eq(SysResourceEntity::getUrl, entity.getUrl())
                .ne(SysResourceEntity::getId, entity.getId());
        if (count(resourceWrapper) > 0) {
            Asserts.fail("[sys.url.exist]");
        }
        return updateById(entity);
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public SysResourceEntity getResource(Long id) {
        return getById(id);
    }

    @Override
    public List<SysResourceEntity> listResource(ResourceListQuery query) {
        QueryWrapper<SysResourceEntity> resourceWrapper = new QueryWrapper<>();
        //资源名称
        if (StrUtil.isNotBlank(query.getName())) {
            resourceWrapper.lambda().like(SysResourceEntity::getName, query.getName());
        }
        //资源URL
        if (StrUtil.isNotBlank(query.getUrl())) {
            resourceWrapper.lambda().like(SysResourceEntity::getUrl, query.getUrl());
        }
        //资源分类ID
        if (query.getCategoryId()!=null) {
            resourceWrapper.lambda().eq(SysResourceEntity::getCategoryId, query.getCategoryId());
        }
        return list(resourceWrapper);
    }
}
